package mesweb.mesweb.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farvision.common.api.CommonResult;
import com.farvision.common.exception.ApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mesweb.mesweb.entity.MesBasicActionType;
import mesweb.mesweb.service.IMesBasicActionTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author niufw
 * @since 2021-05-19
 */
@RestController
@Api(tags="动作类型")
@RequestMapping("/mes/mesbasicactiontype")
public class MesBasicActionTypeController {
    @Autowired
    private IMesBasicActionTypeService mesBasicActionTypeService;

    @GetMapping("/GetAll")
    @ApiOperation("获取动作类型列表")
    public CommonResult<List<MesBasicActionType>> GetAll(@ApiParam("动作类型名称") @RequestParam( value = "name",required = false) String name) {
        List<MesBasicActionType> data = new ArrayList<>();
        if (StringUtils.isBlank(name) || name.equals("全部")) {
            data = mesBasicActionTypeService.list();
        } else {
            QueryWrapper<MesBasicActionType> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("Basic_Action_Type_Name", name);
            data = mesBasicActionTypeService.list(queryWrapper);
        }
        return CommonResult.success(data);
    }

    /**
     * 分页列表查询
     *
     * @param mesBasicActionType
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value="动作类型-分页列表查询", notes="动作类型-分页列表查询")
    @GetMapping(value = "/list")
    public CommonResult<?> queryPageList(MesBasicActionType mesBasicActionType,
                                         @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                         @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                         HttpServletRequest req) {
        QueryWrapper<MesBasicActionType> queryWrapper = new QueryWrapper<>();
        Page<MesBasicActionType> page = new Page<MesBasicActionType>(pageNo, pageSize);
        IPage<MesBasicActionType> pageList = mesBasicActionTypeService.page(page, queryWrapper);
        return CommonResult.success(pageList);
    }

    /**
     * 添加
     *
     * @param mesBasicActionType
     * @return
     */
    @ApiOperation(value="动作类型-添加", notes="动作类型-添加")
    @PostMapping(value = "/save")
    public CommonResult<?> add(@RequestBody MesBasicActionType mesBasicActionType) {
        QueryWrapper<MesBasicActionType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("basic_action_type_No", mesBasicActionType.getBasicActionTypeNo());
        queryWrapper.or().eq("basic_action_type_Name", mesBasicActionType.getBasicActionTypeName());
        MesBasicActionType mesTechnologyFlow1 = mesBasicActionTypeService.getOne(queryWrapper);
        if (mesTechnologyFlow1 != null) {
            String msg = StrUtil.format("编号{}或名称{}已经存在！", mesTechnologyFlow1.getBasicActionTypeNo(), mesTechnologyFlow1.getBasicActionTypeName());
            throw new ApiException(msg);
        }
        mesBasicActionTypeService.save(mesBasicActionType);
        return CommonResult.success("添加成功！");
    }

    /**
     * 编辑
     *
     * @param mesBasicActionType
     * @return
     */
    @ApiOperation(value="动作类型-编辑", notes="动作类型-编辑")
    @PutMapping(value = "/update")
    public CommonResult<?> edit(@RequestBody MesBasicActionType mesBasicActionType) {

        boolean result = false;
        if(mesBasicActionType.id == 0L) {
            QueryWrapper<MesBasicActionType> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("basic_action_type_No", mesBasicActionType.getBasicActionTypeNo());
            queryWrapper.or().eq("basic_action_type_Name", mesBasicActionType.getBasicActionTypeName());
            MesBasicActionType mesTechnologyFlow1 = mesBasicActionTypeService.getOne(queryWrapper);
            if (mesTechnologyFlow1 != null) {
                String msg = StrUtil.format("编号{}或名称{}已经存在！", mesTechnologyFlow1.getBasicActionTypeNo(), mesTechnologyFlow1.getBasicActionTypeName());
                throw new ApiException(msg);
            }
            result = mesBasicActionTypeService.save(mesBasicActionType);
        }
        else
        {
            result = mesBasicActionTypeService.updateById(mesBasicActionType);
        }
        return CommonResult.success(result);
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value="动作类型-通过id删除", notes="动作类型-通过id删除")
    @DeleteMapping(value = "/delete")
    public CommonResult<?> delete(@RequestParam(name="id",required=true) String id) {
        mesBasicActionTypeService.removeById(id);
        return CommonResult.success("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value="动作类型-批量删除", notes="动作类型-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public CommonResult<?> deleteBatch(@RequestBody List<Long> ids) {
        this.mesBasicActionTypeService.removeByIds(ids);
        return CommonResult.success("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value="动作类型-通过id查询", notes="动作类型-通过id查询")
    @GetMapping(value = "/queryById")
    public CommonResult<?> queryById(@RequestParam(name="id",required=true) String id) {
        MesBasicActionType mesBasicActionType = mesBasicActionTypeService.getById(id);
        return CommonResult.success(mesBasicActionType);
    }
}
